﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Net;
using System.Collections.Specialized;
using System.Linq;
using PSAMMatrixCloud.Common;
using System.Reflection;
using Microsoft.AspNetCore.Http;

namespace PSAMMatrixCloud.Common.HttpParameterCommon
{
    /// <summary>
    /// 类名：Core
    /// 功能：Http参数接口公用函数类
    /// 详细：该类是请求、通知返回两个文件所调用的公用函数核心处理文件，不需要修改
    /// </summary>
    public class HttpParameterCore
    {
        /// <summary>
        /// 获取Post过来通知消息，并以“参数名=参数值”的形式组成数组
        /// </summary>
        /// <returns>request回来的信息组成的数组</returns>
        public static SortedDictionary<string, string> GetRequestPost(HttpContext context)
        {
            SortedDictionary<string, string> ParadDictionary = new SortedDictionary<string, string>();
            //Load Form variables into NameValueCollection variable.
            if (context.Request.QueryString.HasValue)
            {
                IQueryCollection QueryPara = context.Request.Query;
                ICollection<string> requestQuery = QueryPara.Keys;
                foreach (var item in requestQuery)
                {
                    ParadDictionary.Add(item, QueryPara[item]);
                }
            }
            if (context.Request.HasFormContentType)
            {
                IFormCollection FormPara = context.Request.Form;
                ICollection<string> requestForm = FormPara.Keys;
                foreach (var item in requestForm)
                {
                    ParadDictionary.Add(item, FormPara[item]);
                }
            }

            return ParadDictionary;
        }

        /// <summary>
        /// 生成签名结果
        /// </summary>
        /// <param name="sArray">要签名的数组</param>
        /// <param name="key">安全校验码</param>
        /// <param name="sign_type">签名类型</param>
        /// <param name="_input_charset">编码格式</param>
        /// <returns>签名结果字符串</returns>
        public static string BuildMysign(Dictionary<string, string> dicArray, string key)
        {
            string prestr = CreateLinkString(dicArray);  //把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串

            prestr = prestr + key;                      //把拼接后的字符串再与安全校验码直接连接起来
            string mysign = CryptoHelper.GetMD5(prestr);    //把最终的字符串签名，获得签名结果
            return mysign;
        }

        /// <summary>
        /// 获取属性字典
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static SortedDictionary<string, string> GetDic(object obj)
        {
            SortedDictionary<string, string> parameters = new SortedDictionary<string, string>();

            IEnumerable<PropertyInfo> properties = obj.GetType().GetRuntimeProperties();
            foreach (var property in properties)
            {
                string name = property.Name;
                object value = property.GetValue(obj);
                parameters.Add(name, value?.ToString());
            }

            return parameters;
        }

        /// <summary>
        /// 除去数组中的空值和签名参数并以字母a到z的顺序排序
        /// </summary>
        /// <param name="dicArrayPre">过滤前的参数组</param>
        /// <returns>过滤后的参数组</returns>
        public static Dictionary<string, string> FilterPara(SortedDictionary<string, string> dicArrayPre)
        {
            Dictionary<string, string> dicArray = new Dictionary<string, string>();
            foreach (KeyValuePair<string, string> temp in dicArrayPre)
            {
                if (temp.Key.ToLower() != "sign" && temp.Key.ToLower() != "sign_type" && temp.Value != "" && temp.Value != null)
                {
                    dicArray.Add(temp.Key.ToLower(), temp.Value);
                }
            }

            return dicArray;
        }

        /// <summary>
        /// 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
        /// </summary>
        /// <param name="sArray">需要拼接的数组</param>
        /// <returns>拼接完成以后的字符串</returns>
        public static string CreateLinkString(Dictionary<string, string> dicArray)
        {
            var sPara = dicArray.Select(p => p.Key + "=" + p.Value);//组合key=value
            string prestr = String.Join("&", sPara);//用&字符拼接各个键值对

            return prestr;
        }
    }
}
